package com.tarena.lbs.business.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.param.BusinessUpdateStatusParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.PictureVO;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.tarena.lbs.business.pojo.po.StorePO;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Repository
public class BusinessRepositoryImpl implements BusinessRepository {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private StoreMapper storeMapper;


    @Override
    public void insertBusiness(BusinessPO businessPO) {
        businessMapper.insert(businessPO);
    }

    @Override
    public BusinessPO getBusinessByName(String businessName) {
        QueryWrapper<BusinessPO> query = new QueryWrapper<>();
        query.eq("business_name",businessName);
        return businessMapper.selectOne(query);
    }
    //分页查询
    @Override
    public List<BusinessPO> selectPage(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper=assembleBusinessQuery(query);
       //2.开启分页插件
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
////         设置分页参数为每页1条记录
//        PageHelper.startPage(query.getPageNo(), 1);
        //3.查询业务列表 被mybatis拦截 查询分页
        List<BusinessPO> pos = businessMapper.selectList(queryWrapper);
        return pos;
    }

    //获取商家详情
    @Override
    public BusinessPO getBusinessById(Integer businessId) {
        if (businessId == null) {
            return null;
        }
        return businessMapper.selectById(businessId);
    }

    //获取商家下的门店列表
    @Override
    public List<StoreVO> getStoreByBusinessId(Integer businessId) {
        if (businessId == null) {
            return new ArrayList<>();
        }
        // 查询该商家下的所有店铺
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);
        List<StorePO> storePOs = storeMapper.selectList(queryWrapper);
        log.debug("查询商家ID为{}的店铺数量: {}", businessId, storePOs.size());
        // 转换为 StoreVO 列表
        List<StoreVO> storeVOs = new ArrayList<>();
        for (StorePO storePO : storePOs) {
            StoreVO storeVO = new StoreVO();
            BeanUtils.copyProperties(storePO, storeVO);

            // 设置图片信息 - 如果 storeImagesId 不为空
            if (storePO.getStoreImagesId() != null && !storePO.getStoreImagesId().isEmpty()) {
                List<PictureVO> pictureVOS = new ArrayList<>();
                // 假设 storeImagesId 是逗号分隔的图片ID
                String[] imageIds = storePO.getStoreImagesId().split(",");
                for (String imageId : imageIds) {
                    if (!imageId.trim().isEmpty()) {
                        PictureVO pictureVO = new PictureVO();
                        pictureVO.setId(Integer.valueOf(imageId.trim()));
                        // 设置图片访问路径
                        pictureVO.setImage("http://localhost:9081/static/" + imageId.trim());
                        pictureVOS.add(pictureVO);
                    }
                }
                storeVO.setPictureVOS(pictureVOS);
            }

            // 设置 storeLogoPic
            if (storePO.getStoreLogo() != null && !storePO.getStoreLogo().isEmpty()) {
                storeVO.setStoreLogoPic("http://localhost:9081/static/" + storePO.getStoreLogo());
            }

            storeVOs.add(storeVO);
        }
        return storeVOs;
    }

    @Override
    public BusinessPO selectById(Integer businessId) {
        return businessMapper.selectById(businessId);
    }

    @Override
    public void updateById(BusinessPO po) {
        businessMapper.updateById(po);
    }

    @Override
    public void updateStatus(BusinessUpdateStatusParam param) {
        UpdateWrapper<BusinessPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", param.getId());
        updateWrapper.set("business_status", param.getBusinessStatus());
        updateWrapper.set("audit_remarks", param.getAuditRemarks());
        businessMapper.update(updateWrapper);
    }


    private QueryWrapper<BusinessPO> assembleBusinessQuery(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        if (query != null) {
            // 商家名称模糊查询
            if (query.getBusinessName() != null && !query.getBusinessName().isEmpty()) {
                queryWrapper.like("business_name", query.getBusinessName());
            }
            // 负责人电话精确查询
            if (query.getBusinessHeadPhone() != null && !query.getBusinessHeadPhone().isEmpty()) {
                queryWrapper.eq("business_head_phone", query.getBusinessHeadPhone());
            }
            // 商家状态查询
            if (query.getBusinessStatus() != null) {
                queryWrapper.eq("business_status", query.getBusinessStatus());
            }
            // 时间范围查询 - 开始时间
            if (query.getStartingTime() != null) {
                queryWrapper.ge("entry_time", query.getStartingTime());
            }
            // 时间范围查询 - 结束时间
            if (query.getEndTime() != null) {
                queryWrapper.le("entry_time", query.getEndTime());
            }
            // 商家ID精确查询
            if (query.getBusinessId() != null) {
                queryWrapper.eq("id", query.getBusinessId());
            }
        }
        // 默认按ID降序排序
        queryWrapper.orderByDesc("id");
        return queryWrapper;
    }
}
